﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;

namespace CommonUtils
{
    /// <summary>
    /// 资源读取
    /// </summary>
    public static class ResourceUtil
    {
        /// <summary>
        /// 生成读取器
        /// </summary>
        public static ResourceReader GetReader(Stream stream)
        => new ResourceReader(stream);

        /// <summary>
        /// 获取读取器数据
        /// </summary>
        public static byte[] ReadBytes(this ResourceReader reader, string name)
        {
            reader.GetResourceData(name, out _, out byte[] data);
            return data;
        }

        public static Stream Get(Assembly assembly, string name)
        {
            var path = assembly.GetManifestResourceNames().FirstOrDefault(m => m.Contains(name));
            //一定要将资源属性设置为[嵌入的资源]
            var stream = assembly.GetManifestResourceStream(path);
            if (stream == null)
                throw new ExceptionPlus("读取资源文件失败：" + path);
            return stream;
        }

        /// <summary>
        /// Resources不用打 直接打Resources文件夹下的文件名 生成操作: 嵌入的资源
        /// </summary>
        public static string GetText(Assembly assembly, string path)
        => Get(assembly, path).GetText();

        public static string GetText(Type type, string path)
        => Get(type.Assembly, path).GetText();

        /// <summary>
        /// 获取公用类库嵌入的资源
        /// </summary>
        public static Stream GetFromCommon(ResourceName resourceName)
        {
            string name = resourceName.ToString();
            switch (resourceName)
            {
                case ResourceName.IpDivisionTxt:
                    name = "ip-division.txt";
                    break;
                case ResourceName.LoadingGif:
                    name = "loading.gif";
                    break;
                case ResourceName.MissingJpg:
                    name = "missing.jpg";
                    break;
                case ResourceName.MissingPng:
                    name = "missing.png";
                    break;
                case ResourceName.MissingMinJpg:
                    name = "missing-min.jpg";
                    break;
                case ResourceName.SqliteDb:
                    name = "sqlite.db";
                    break;
                case ResourceName.TaijiPng:
                    name = "Taiji.png";
                    break;
                case ResourceName.TestTxt:
                    name = "test.txt";
                    break;
            }
            return Get(ReflectionUtil.CommonAssembly, name);
        }

        /// <summary>
        /// 获取公用类库嵌入的资源
        /// </summary>
        public static string GetTextFromCommon(string resourceName)
        => GetText(ReflectionUtil.CommonAssembly, resourceName);

        /// <summary>
        /// 获取公用类库嵌入的资源
        /// </summary>
        public static string GetTextFromCommon(ResourceName resourceName)
        => GetFromCommon(resourceName).GetText();

        public static IpDivision[] GetIpDivisions()
        {
            var text = GetTextFromCommon(ResourceName.IpDivisionTxt);
            var lines = text.GetLines();
            var divisions = new List<IpDivision>();
            foreach (var line in lines)
            {
                var cells = line.Split(" ");
                if (cells.Length < 4)
                    continue;
                var division = new IpDivision();
                division.Start = cells[0];
                division.End = cells[2];
                division.Belong = cells[3];
                divisions.Add(division);
            }
            return divisions.ToArray();
        }

        public static string NoPicBase64()
        {
            return EncodeUtil.Base64Encode(GetFromCommon(ResourceName.MissingMinJpg));
        }

        public static string TaijiPicBase64()
        {
            return EncodeUtil.Base64Encode(GetFromCommon(ResourceName.TaijiPng));
        }
    }
}
